<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Itools
 *
 * @author sunday add 2014.5.28  modify by dragon.hu @2016.2.18
 */
require_once 'BaseChannel.php';

//itools平台
class Itools implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		$paymentKey="NIhmYdfPe05f"; // 
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		$paymentKey="NIhmYdfPe05f"; // 
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		require_once('include/itools/notify.class.php');
		$orderLog = new ChargeOrder();
		$orderLog->log("itools", $_REQUEST);
		
		if (!isset($_REQUEST['notify_data']) || !isset($_REQUEST['sign'])) 
		{
			echo 'itools','can not get notify_data or sign data....';
			//die('fail');
			return 'fail';
		}
		
		//通知数据
		$notify_data = $_REQUEST['notify_data'];
		
		//签名
		$sign = $_REQUEST['sign'];
		
		$notify = new notify();
		
		//RSA解密
		$notify_data = $notify->decrypt($notify_data);
		
		//for test 
		//$notify_data='{"order_id_com":"2013050900000712","user_id":"10010","amount":"0.10","account":"test001","order_id":"2013050900000713","result":"success"}';
		//if(1==1)
		
		//验证签名
		if ($notify->verify($notify_data, $sign))	
		{
			//逻辑处理, $notify_data: json数据(格式: {"order_id_com":"2013050900000712","user_id":"10010",
			//				"amount":"0.10","account":"test001","order_id":"2013050900000713","result":"success"})
			$json = json_decode($notify_data, true);

			//result为success才是支付成功
			if('success'==$json['result'])
			{
				$status = STATUS_SUCCESS;
			}else 
			{
				$status = STATUS_FAILD;
			}
			if ($platform == ANDROID)
			{
				if(true == $orderLog->recordOrder($status, $json['amount'], $json['order_id_com'], "itools", $platform,$json['order_id']))
				{
					$orderLog->log('itools','platform:'.$platform.'amount:'.$json['amount'].' order_id:'.$json['order_id'].' success');
					$ret = "success";
				}else
				{
					$orderLog->log('itools','platform:'.$platform.'amount:'.$json['amount'].' order_id:'.$json['order_id'].' fail');
					//echo " order:".$json['order_id_com']." insert database fail";
					//$orderLog->log(" order:".$json['order_id_com']." insert database fail");
					$ret = "fail";
				}
			}else if ($platform == IOS)
			{
				if(true == $orderLog->recordOrder($status, $json['amount'], $json['order_id_com'], "IosITools", $platform,$json['order_id']))
				{
					$orderLog->log('itools','platform:'.$platform.'amount:'.$json['amount'].' order_id:'.$json['order_id'].' success');
					$ret = "success";
				}else
				{
					$orderLog->log('itools','platform:'.$platform.'amount:'.$json['amount'].' order_id:'.$json['order_id'].' fail');
					//echo " order:".$json['order_id_com']." insert database fail";
					//$orderLog->log(" order:".$json['order_id_com']." insert database fail");
					$ret = "fail";
				}
			}
			
		} else 
		{
			//echo " order:".$json['order_id_com']." sign verify fail";
			$orderLog->log('itools'," order:".$json['order_id_com']." sign verify fail");
			$ret = "fail";
		}	
		return $ret;		
	}	
}